package other;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: javase
 * @description: 之字形输出
 * @author: lld
 * @create: 2021-04-27 23:09
 **/
class ZigZagConversion6 {
    public static void main(String[] args) {
        String abcdefg3 = new ZigZagConversion6().convert("ABCDEFGHIJ", 3);
        String abcdefg4 = new ZigZagConversion6().convert("ABCDEFGHIJ", 4);
        System.out.println("abcdefg3 = " + abcdefg3);
        System.out.println("abcdefg4 = " + abcdefg4);
    }
    public String convert(String s, int numRows) {
        if(numRows < 2) return s;
        List<StringBuilder> rows = new ArrayList<StringBuilder>();
        for(int i = 0; i < numRows; i++) rows.add(new StringBuilder());
        int i = 0, flag = -1;
        for(char c : s.toCharArray()) {
            rows.get(i).append(c);
            if(i == 0 || i == numRows -1) flag = - flag;
            i += flag;
        }
        StringBuilder res = new StringBuilder();
        for(StringBuilder row : rows) res.append(row);
        return res.toString();
    }
}
